<template>
  <Container>
    <template slot="top">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="流程名称" prop="processName">
          <el-input
            v-model="queryParams.processName"
            placeholder="请输入流程名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="发起人" prop="originatorId">
          <el-select v-model="queryParams.originatorId" placeholder="请选择发起人" clearable filterable >
            <el-option
                v-for="user in userSelectData"
                :key="user.value"
                :label="user.label"
                :value="user.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="readFlag">
          <el-select v-model="queryParams.state" placeholder="状态" clearable>
            <el-option
              :key="'已读'"
              :label="'已读'"
              :value="'已读'"
            />
            <el-option
              :key="'未读'"
              :label="'未读'"
              :value="'未读'"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            v-hasPermi="['workflow:process:copyExport']"
            @click="handleExport"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </template>
    <template slot="center">
      <el-table v-loading="loading" :data="copyList" @selection-change="handleSelectionChange" :border="true" height="100%">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="抄送编号" align="center" prop="copyId" />
        <el-table-column label="标题" align="center" prop="title" :show-overflow-tooltip="true" />
        <el-table-column label="流程名称" align="center" prop="processName" :show-overflow-tooltip="true" />
        <el-table-column label="发起人" align="center" prop="originatorName" />
        <el-table-column label="状态" align="center" prop="state" />
        <el-table-column label="创建时间" align="center" prop="createTime">
          <template slot-scope="scope">
            <span>{{ $parseTime(scope.row.createTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-tickets"
              @click="handleFlowRecord(scope.row)"
              v-hasPermi="['workflow:process:query']"
            >详情</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
    <template slot="bottom">
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </template>
  </Container>
</template>

<script>
import { listCopyProcess, updateCcReaded,delCopy } from "@/api/workflow/process"
import {userSelect} from "@/api/system/user";

export default {
  name: "Copy",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 流程抄送表格数据
      copyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        processId: undefined,
        processName: undefined,
        categoryId: undefined,
        taskId: undefined,
        userId: undefined,
        originatorId: undefined,
        state:"未读"
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        copyId: [
          { required: true, message: "抄送主键不能为空", trigger: "blur" }
        ],
        processId: [
          { required: true, message: "流程主键不能为空", trigger: "blur" }
        ],
        processName: [
          { required: true, message: "流程名称不能为空", trigger: "blur" }
        ],
        categoryId: [
          { required: true, message: "流程分类主键不能为空", trigger: "blur" }
        ],
        taskId: [
          { required: true, message: "任务主键不能为空", trigger: "blur" }
        ],
        userId: [
          { required: true, message: "用户主键不能为空", trigger: "blur" }
        ]
      },
      userSelectData:[]
    };
  },
  created() {
    this.getUserSelect();
    this.getList();
  },
  methods: {
    /**
     * 查询用户下拉数据
     */
    getUserSelect(){
      userSelect().then(response => {
        this.userSelectData=response.data;
      });
    },
    /** 查询流程抄送列表 */
    getList() {
      this.loading = true;
      listCopyProcess(this.queryParams).then(response => {
        this.copyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /**
     * 取消按钮
     */
    cancel() {
      this.open = false;
      this.reset();
    },
    /**
     * 表单重置
     */
    reset() {
      this.form = {
        copyId: undefined,
        processId: undefined,
        processName: undefined,
        categoryId: undefined,
        taskId: undefined,
        userId: undefined,
        originatorName: undefined,
        createUserId: undefined,
        createTime: undefined,
        updateUserId: undefined,
        updateTime: undefined,
        state: undefined,
        isDel: undefined
      };
      this.$resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.$resetForm("queryForm");
      this.handleQuery();
    },
    /**
     * 多选框选中数据
     * @param selection
     */
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.copyId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 查看详情 */
    handleFlowRecord(row){
      console.log(row);
      updateCcReaded({ id: row.copyId }).then(res => {
        if (res.success) {
          console.log(res);
        }
      })
      this.$router.push({
        path: '/workflow/task/detail/' + row.instanceId,
        query: {
          taskId: row.taskId,
          processed: false
        }
      })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$downloadAction('/workflow/process/copyExport', {
        ...this.queryParams
      }, `wf_copy_process_${new Date().getTime()}.xlsx`)
    },
    /**
     * 删除按钮操作
     * @param row
     */
    handleDelete(row) {
      const copyIds = row.copyId
      this.$modal.confirm('是否确认删除公告编号为"' + copyIds + '"的数据项？').then(function() {
        return delCopy(copyIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
  }
};
</script>
<style lang="scss" scoped>

</style>
